<template>
  <ContentWrap>
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="page-title">
          <Icon icon="ep:document-checked" class="title-icon" />
          <div class="title-text">
            <h2>跟进记录</h2>
            <p>管理客户跟进记录和外呼状态</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 搜索工作栏 -->
    <div class="search-container">
      <el-form
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="100px"
        label-position="left"
        class="search-form"
      >
        <div class="search-row">
          <el-form-item label="跟进类型" prop="type" class="search-item">
            <el-select
              v-model="queryParams.type"
              placeholder="请选择跟进类型"
              clearable
              class="search-select"
            >
              <el-option label="备注" value="remark">
                <div class="option-item">
                  <Icon icon="ep:edit" class="option-icon" />
                  <span>备注</span>
                </div>
              </el-option>
              <el-option label="标记" value="tag">
                <div class="option-item">
                  <Icon icon="ep:price-tag" class="option-icon" />
                  <span>标记</span>
                </div>
              </el-option>
              <el-option label="外呼" value="call">
                <div class="option-item">
                  <Icon icon="ep:phone" class="option-icon" />
                  <span>外呼</span>
                </div>
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="号码" prop="number" class="search-item">
            <el-input
              v-model="queryParams.number"
              placeholder="请输入号码"
              clearable
              @keyup.enter="handleQuery"
              class="search-input"
            >
              <template #prefix>
                <Icon icon="ep:phone" class="input-icon" />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="标记内容" prop="tag" class="search-item">
            <el-input
              v-model="queryParams.tag"
              placeholder="请输入标记内容"
              clearable
              @keyup.enter="handleQuery"
              class="search-input"
            >
              <template #prefix>
                <Icon icon="ep:price-tag" class="input-icon" />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="跟进内容" prop="remark" class="search-item">
            <el-input
              v-model="queryParams.remark"
              placeholder="请输入跟进内容"
              clearable
              @keyup.enter="handleQuery"
              class="search-input"
            >
              <template #prefix>
                <Icon icon="ep:edit" class="input-icon" />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="创建时间" prop="createTime" class="search-item">
            <el-date-picker
              v-model="queryParams.createTime"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="daterange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
              class="search-date-picker"
            >
              <template #prefix-icon>
                <Icon icon="ep:calendar" class="input-icon" />
              </template>
            </el-date-picker>
          </el-form-item>
        </div>

        <div class="search-actions">
          <el-button class="search-btn" @click="handleQuery">
            <Icon icon="ep:search" class="btn-icon" />
            <span class="btn-text">搜索</span>
          </el-button>
          <el-button class="reset-btn" @click="resetQuery">
            <Icon icon="ep:refresh" class="btn-icon" />
            <span class="btn-text">重置</span>
          </el-button>
        </div>
      </el-form>
    </div>

    <!-- 列表容器 -->
    <div class="table-container">
      <el-table v-loading="loading" :data="list" :stripe="true" class="follow-table">
        <el-table-column label="跟进类型" align="center" prop="type" width="120">
          <template #default="scope">
            <div class="type-cell">
              <el-tag
                :type="item.colorType"
                v-for="(item, index) in getType(scope.row.type)"
                :key="index"
                class="type-tag"
              >
                <Icon :icon="getTypeIcon(scope.row.type)" class="tag-icon" />
                {{ item.label }}
              </el-tag>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="号码" align="center" prop="number" min-width="150">
          <template #default="scope">
            <div class="number-cell">
              <Icon icon="ep:phone" class="number-icon" />
              <span class="number-text">{{ scope.row.number }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="标记内容" align="center" width="220" prop="tag">
          <template #default="scope">
            <div class="tag-container">
              <div class="tag-list" :class="{ 'has-more': scope.row.tagList.length > 2 }">
                <EntTag
                  v-for="tag in scope.row.tagList.slice(0, 2)"
                  :key="tag.id"
                  :content="tag.content"
                  :bgColor="tag.bgColor"
                  :textColor="tag.textColor"
                  class="ent-tag"
                />
              </div>
              <div class="more-indicator" v-if="scope.row.tagList.length > 2">
                <el-tooltip
                  :content="`还有 ${scope.row.tagList.length - 2} 个标签`"
                  placement="top"
                >
                  <el-tag size="small" type="info" class="more-tag">
                    <Icon icon="ep:more-filled" class="more-icon" />
                    +{{ scope.row.tagList.length - 2 }}
                  </el-tag>
                </el-tooltip>
              </div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="跟进内容" align="center" prop="remark" min-width="200">
          <template #default="scope">
            <div class="remark-cell" v-if="scope.row.remark">
              <Icon icon="ep:edit" class="remark-icon" />
              <span class="remark-text">{{ scope.row.remark }}</span>
            </div>
            <span v-else class="no-data">-</span>
          </template>
        </el-table-column>

        <el-table-column label="外呼状态" align="center" prop="callStatus" width="120">
          <template #default="scope">
            <div class="status-cell">
              <el-tag :type="scope.row.callStatus ? 'success' : 'danger'" class="status-tag">
                <Icon :icon="scope.row.callStatus ? 'ep:check' : 'ep:close'" class="status-icon" />
                {{ scope.row.callStatus ? '接通' : '未接通' }}
              </el-tag>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="负责人编号" align="center" prop="userId" width="120">
          <template #default="scope">
            <div class="user-cell">
              <Icon icon="ep:user" class="user-icon" />
              <span class="user-text">{{ scope.row.userId }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="创建时间" align="center" prop="createTime" width="200">
          <template #default="scope">
            <div class="time-cell">
              <Icon icon="ep:clock" class="time-icon" />
              <span class="time-text">{{
                dateFormatter(scope.row, scope.column, scope.row.createTime)
              }}</span>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
          class="custom-pagination"
        />
      </div>
    </div>
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <FollowRecordForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as FollowRecordApi from '@/api/ent/followrecord'
import FollowRecordForm from './FollowRecordForm.vue'
import { DICT_TYPE, getStrDictOptions } from '@/utils/dict'
import { MoreFilled } from '@element-plus/icons-vue'
defineOptions({ name: 'FollowRecord' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  type: undefined,
  number: undefined,
  tag: undefined,
  remark: undefined,
  callStatus: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const getType = (type) => {
  const res = getStrDictOptions(DICT_TYPE.ENT_FOLLOW_TYPE).filter((item) => item.value == type)
  return res
}

// 获取类型图标
const getTypeIcon = (type) => {
  const iconMap = {
    remark: 'ep:edit',
    tag: 'ep:price-tag',
    call: 'ep:phone'
  }
  return iconMap[type] || 'ep:document'
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await FollowRecordApi.getFollowRecordPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await FollowRecordApi.exportFollowRecord(queryParams)
    download.excel(data, '跟进记录.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
// 页面头部
.page-header {
  margin-bottom: 24px;
  padding: 12px 24px;
  background: linear-gradient(135deg, #667eea 0%, #0052cc 100%);
  border-radius: 16px;
  color: white;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.page-title {
  display: flex;
  align-items: center;
  gap: 16px;
}

.title-icon {
  font-size: 32px;
  color: rgba(255, 255, 255, 0.9);
}

.title-text {
  h2 {
    font-size: 24px;
    font-weight: 600;
    margin: 0 0 4px 0;
    color: white;
  }

  p {
    font-size: 14px;
    margin: 0;
    color: rgba(255, 255, 255, 0.8);
  }
}

// 搜索容器
.search-container {
  background: white;
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 1px solid #e5e7eb;
}

.search-form {
  .search-row {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    margin-bottom: 20px;
  }

  .search-item {
    flex: 1;
    min-width: 250px;
    margin-bottom: 0;

    :deep(.el-form-item__label) {
      font-weight: 500;
      color: #374151;
    }
  }
}

.search-input {
  :deep(.el-input__wrapper) {
    border-radius: 8px;
    border: 2px solid #e5e7eb;
    transition: all 0.3s ease;

    &:hover {
      border-color: #d1d5db;
    }

    &.is-focus {
      border-color: #667eea;
      box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    }
  }

  .input-icon {
    color: #9ca3af;
    font-size: 16px;
  }
}

.search-select {
  width: 100%;

  :deep(.el-select__wrapper) {
    border-radius: 8px;
    border: 2px solid #e5e7eb;
    transition: all 0.3s ease;

    &:hover {
      border-color: #d1d5db;
    }

    &.is-focused {
      border-color: #667eea;
      box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    }
  }
}

.search-date-picker {
  width: 100%;

  :deep(.el-input__wrapper) {
    border-radius: 8px;
    border: 2px solid #e5e7eb;
    transition: all 0.3s ease;

    &:hover {
      border-color: #d1d5db;
    }

    &.is-focus {
      border-color: #667eea;
      box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    }
  }

  .input-icon {
    color: #9ca3af;
    font-size: 16px;
  }
}

.option-item {
  display: flex;
  align-items: center;
  gap: 8px;

  .option-icon {
    font-size: 14px;
    color: #6b7280;
  }
}

.search-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  flex-wrap: wrap;
}

.search-btn,
.reset-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 20px;
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;

  .btn-icon {
    font-size: 16px;
  }

  &:hover {
    transform: translateY(-1px);
  }
}

.search-btn {
  background: #f3f4f6;
  border: 2px solid #e5e7eb;
  color: #374151;

  &:hover {
    background: #e5e7eb;
    border-color: #d1d5db;
  }
}

.reset-btn {
  background: #fef3c7;
  border: 2px solid #fbbf24;
  color: #92400e;

  &:hover {
    background: #fde68a;
    border-color: #f59e0b;
  }
}

// 表格容器
.table-container {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 1px solid #e5e7eb;
}

.follow-table {
  :deep(.el-table__header) {
    background: #f8fafc;

    th {
      background: #f8fafc !important;
      color: #374151;
      font-weight: 600;
      border-bottom: 2px solid #e5e7eb;
      padding: 16px 12px;
    }
  }

  :deep(.el-table__body) {
    tr {
      transition: all 0.2s ease;

      &:hover {
        background: #f8fafc !important;
      }

      td {
        padding: 16px 12px;
        border-bottom: 1px solid #f1f5f9;
      }
    }
  }
}

// 表格单元格样式
.type-cell {
  display: flex;
  justify-content: center;

  .type-tag {
    display: flex;
    align-items: center;
    gap: 4px;
    border-radius: 6px;
    font-weight: 500;

    .tag-icon {
      font-size: 12px;
    }
  }
}

.number-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;

  .number-icon {
    color: #10b981;
    font-size: 16px;
  }

  .number-text {
    font-weight: 500;
    color: #374151;
    font-family: 'Courier New', monospace;
  }
}

.tag-container {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;

  .tag-list {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    flex: 1;

    .ent-tag {
      font-size: 12px;
      padding: 2px 6px;
      border-radius: 4px;
    }
  }

  .more-indicator {
    .more-tag {
      display: flex;
      align-items: center;
      gap: 2px;
      cursor: pointer;

      .more-icon {
        font-size: 10px;
      }
    }
  }
}

.remark-cell {
  display: flex;
  align-items: flex-start;
  gap: 8px;

  .remark-icon {
    color: #8b5cf6;
    font-size: 16px;
    margin-top: 2px;
    flex-shrink: 0;
  }

  .remark-text {
    color: #374151;
    line-height: 1.4;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
}

.status-cell {
  display: flex;
  justify-content: center;

  .status-tag {
    display: flex;
    align-items: center;
    gap: 4px;
    border-radius: 6px;
    font-weight: 500;

    .status-icon {
      font-size: 12px;
    }
  }
}

.user-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;

  .user-icon {
    color: #6366f1;
    font-size: 14px;
  }

  .user-text {
    color: #6b7280;
    font-weight: 500;
  }
}

.time-cell {
  display: flex;
  align-items: center;
  gap: 6px;

  .time-icon {
    color: #f59e0b;
    font-size: 14px;
  }

  .time-text {
    color: #6b7280;
    font-size: 13px;
  }
}

.no-data {
  color: #9ca3af;
  font-style: italic;
}

// 分页容器
.pagination-container {
  display: flex;
  justify-content: center;
  padding: 24px;
  border-top: 1px solid #f1f5f9;
}

.custom-pagination {
  :deep(.el-pagination) {
    .el-pager li {
      border-radius: 6px;
      margin: 0 2px;

      &.is-active {
        background: #667eea;
        color: white;
      }
    }

    .btn-prev,
    .btn-next {
      border-radius: 6px;
    }
  }
}

// H5 响应式设计
@media (max-width: 768px) {
  .page-header {
    padding: 12px 20px;
    margin-bottom: 16px;

    .title-text {
      h2 {
        font-size: 20px;
      }

      p {
        font-size: 13px;
      }
    }
  }

  .search-container {
    padding: 16px;

    .search-row {
      flex-direction: column;
      gap: 16px;

      .search-item {
        min-width: auto;
      }
    }

    .search-actions {
      justify-content: stretch;

      .search-btn,
      .reset-btn {
        flex: 1;
        justify-content: center;
      }
    }
  }

  .table-container {
    overflow-x: auto;

    .follow-table {
      min-width: 1000px;
    }
  }

  .tag-container {
    padding: 4px;

    .tag-list {
      .ent-tag {
        font-size: 10px;
        padding: 1px 4px;
      }
    }
  }
}

@media (max-width: 480px) {
  .page-header {
    padding: 12px 16px;

    .page-title {
      gap: 12px;

      .title-icon {
        font-size: 24px;
      }

      .title-text {
        h2 {
          font-size: 18px;
        }
      }
    }
  }

  .search-container {
    padding: 12px;

    .search-actions {
      flex-direction: column;
      gap: 8px;
    }
  }

  .follow-table {
    :deep(.el-table__header) {
      th {
        padding: 12px 8px;
        font-size: 13px;
      }
    }

    :deep(.el-table__body) {
      tr td {
        padding: 12px 8px;
      }
    }
  }

  .btn-text {
    display: none;
  }
}

// 深色模式支持
@media (prefers-color-scheme: dark) {
  .search-container,
  .table-container {
    background: #1e293b;
    border-color: #334155;
  }

  .follow-table {
    :deep(.el-table__header) {
      th {
        background: #0f172a !important;
        color: #f1f5f9;
        border-color: #334155;
      }
    }

    :deep(.el-table__body) {
      tr {
        &:hover {
          background: #334155 !important;
        }

        td {
          border-color: #334155;
        }
      }
    }
  }

  .number-cell .number-text,
  .remark-cell .remark-text,
  .user-cell .user-text,
  .time-cell .time-text {
    color: #f1f5f9;
  }
}
</style>
